<?php
defined('IN_RONCRM') or exit('No permission resources.');
class AdminManageController extends CAdmin {
	
	private $db,$role_db;
	
	public function __construct(){
		parent::__construct();	
		$this->db = new Admin_Model();
		$this->role_db = new Admin_role_Model();
	}
	
	
	public function init(){	
		$infos = $this->db->select('','username,roleid,lastloginip,lastlogintime,email,realname,userid');
		$cache = new Caches();
		$roles = $cache->getcache('role','commons');
		include self::admin_tpl('admin_list');
	}
		
	public function add(){
		if(isset($_POST['dosubmit'])){
			$info = array();
//			if(!$this->checkname($_POST['info']['username'])){
//				self::showmessage('用户已存在'); 
//			}
//			$info = $this->checkuserinfo($_POST['info']);
//			if(!$this->checkpasswd($info['password'])){
//				self::showmessage('密码不合法');
//			}
//			$passwordinfo = $this->password($info['password']);
//			$info['password'] = $passwordinfo['password'];			
//			$info['encrypt'] = $passwordinfo['encrypt'];
//				
//			$admin_fields = array('username', 'email', 'password', 'encrypt','roleid','realname');			
//			foreach ($info as $k => $val){
//				if(!in_array($k, $admin_fields)){
//					unset($info[$k]);
//				}
//			}		
//			$this->db->insert($info);

			
//验证测试开始
$info = $_POST['info'];
if(!$this->db->insert($info)){
	print_r($this->db->getErrors());
}	
//结束


			if($this->db->insert_id()){
				self::showmessage('操作成功！','?m=admin&c=adminmanage');
			}
		} else {
			$roles = $this->role_db->select();					
			include self::admin_tpl('admin_add');
		}
		
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$info = $this->checkuserinfo($_POST['info']);
			if(isset($info['password']) && !empty($info['password'])){
				$this->edit_password($info['userid'], $info['password']);
			}
			$userid = $info['userid'];
			$admin_fields = array('username','email','roleid','realname');
			foreach($info as $k => $val){
				if(!in_array($k,$admin_fields)){
					unset($info[$k]);
				}
			}
			$this->db->update($info,array('userid' => $userid));
			self::showmessage('操作成功！','?m=admin&c=adminmanage');
		} else {
			$info = $this->db->get_one(array('userid'=>$_GET['userid']));
			extract($info);	
			$roles = $this->role_db->select();		
			include self::admin_tpl('admin_edit');		
		}
	}
	
	public function delete(){
//		$userid = intval($_GET['userid']);
//		if($userid == 1){
//			self::showmessage('不能删除这个管理员', HTTP_REFERER);
//		}
//		$user_group_db = new User_group_Model();
//		$user_group_db->delete(array('userid'=>$userid));
//		$this->db->delete(array('userid' => $userid));
//		self::showmessage('操作成功！','?m=admin&c=adminmanage');
	}
	/**
	 * 修改密码
	 * Enter description here ...
	 */
	public function admin_edit_pwd(){
		$userid = $_SESSION['userid'];
		if(isset($_POST['dosubmit'])) {
			$r = $this->db->get_one(array('userid'=>$userid),'password,encrypt');
			if($this->password($_POST['old_password'],$r['encrypt']) != $r['password']) 
			self::showmessage('旧密码不准确',HTTP_REFERER);
			if(isset($_POST['new_password']) && !empty($_POST['new_password'])){
				$this->edit_password($userid, $_POST['new_password']);
			}
			self::showmessage('密码修改成功','?m=admin&c=index&a=login_out');
		}else{
			$info = $this->db->get_one(array('userid'=>$userid));
			extract($info);
			include self::admin_tpl('admin_edit_pwd');	
		}
	}
	
	/**
	 * 修改用户信息
	 */
	public function admin_edit_info(){
		$userid = $_SESSION['userid'];
		if(isset($_POST['dosubmit'])){
			$admin_fields = array('email','realname');
			$info = array();
			$info = $_POST['info'];
			foreach ($info as $k=>$value) {
				if (!in_array($k, $admin_fields)){
					unset($info[$k]);
				}
			}
			$this->db->update($info,array('userid'=>$userid));
			self::showmessage('操作成功！',HTTP_REFERER);
		} else {
			$info = $this->db->get_one(array('userid'=>$userid));
			extract($info);
			include self::admin_tpl('admin_edit_info');
		}
	}
	
	/**
	 * 检查用户名重名
	 */	
	private function checkname($username){
		$username = trim($username);
		if($this->db->get_one(array('username' => $username),'userid')){
			return false;
		}
		return true;
	}
	
	/**
	 * 验证数据
	 */
	private function checkuserinfo($data){
		$validator = new GlobalValidator();
		if(!is_array($data)){
			self::showmessage('参数不完整'); return false;
		} elseif(!$validator->is_username($data['username'])){
			self::showmessage('用户名不合法'); return false;
		} elseif(empty($data['email']) || !$validator->is_email($data['email'])){
			self::showmessage('email不合法'); return false;
		} 
		 elseif(empty($data['roleid'])){
			return false;
		} 
		return $data;
	}
	
	/**
	 * 对用户的密码进行加密
	 * @param $password
	 * @param $encrypt //传入加密串，在修改密码时做认证
	 * @return array/password
	 */
	private function password($password, $encrypt = ''){
		$process = new CProcess(); 
		$pwd = array();
		$pwd['encrypt'] = $encrypt ? $encrypt : $process->create_randonstr();
		$pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']);
		return  $encrypt ? $pwd['password'] : $pwd;
	}
	
	/*
	 * 检查密码
	 */
	private function checkpasswd($password){
		$validator = new GlobalValidator();
		if(!$validator->is_password($password)){
			return false;
		}
		return true;
	}
	
	/*
	 * 修改密码
	 */
	private function edit_password($userid, $password){
		$userid = intval($userid);
		if($userid < 1) return false;
		$validator = new GlobalValidator();
		if(!$validator->is_password($password)){
			self::showmessage('密码不合法');
			return false;
		}
		$passwordinfo = $this->password($password);
		return $this->db->update($passwordinfo,array('userid'=>$userid));
	}
	
	/*
	 * 异步检测用户名
	 */
	public function public_checkname_ajx(){
		$username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit(0);
		if ($this->db->get_one(array('username'=>$username),'userid')){
			exit('0');
		} 
		exit('1');	
	}

}